//
//  LiveChatViewController.m
//  YouinRTCDemo
//
//  Created by lprison on 2021/11/10.
//

#import "LiveChatViewController.h"
#import "LiveChatTableViewCell.h"
#import "ChatImageCell.h"
#import "YBImageBrowser.h"
@interface LiveChatViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)NSTimer *time;
@property (nonatomic,strong)NSMutableDictionary *giftDic;

@end

@implementation LiveChatViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTabLe];
//    self.giftView = [[GiftView alloc] initWithFrame:CGRectMake(-(SCREEN_WIDTH-105), 0, SCREEN_WIDTH-105, 70)];
//    [[UIFactory shareInstace] addTransitionColorLeftToRight:rgba(235, 42, 42, 1) endColor:rgba(235, 42, 42, 0) view:self.giftView];
//    self.giftView.alpha = 0;
//    [self.view addSubview:self.giftView];
    self.messageArray = [NSMutableArray new];
    self.giftDic = [NSMutableDictionary new];
}

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.mas_equalTo(self.view);
        make.top.mas_equalTo(self.view).offset(0);
    }];
}

-(void)setTabLe{
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.showsVerticalScrollIndicator = false;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.estimatedRowHeight = 100;
    _tableView.rowHeight = UITableViewAutomaticDimension;
    [_tableView registerClass:[LiveChatTableViewCell class] forCellReuseIdentifier:@"LiveChatTableViewCell"];
    [_tableView registerClass:[ChatImageCell class] forCellReuseIdentifier:@"ChatImageCell"];
    
    _tableView.tableFooterView = [UIView new];
    [self.view addSubview:_tableView];
    
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    _tableView.transform = CGAffineTransformMakeScale(1, -1);
    [self.view addSubview:_tableView];
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor clearColor];
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 7;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.messageArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
    
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(self.messageArray[indexPath.section].messageType == -1){
        LiveChatTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LiveChatTableViewCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.message = self.messageArray[indexPath.section];
        cell.transform = CGAffineTransformMakeScale(1, -1);
        
        return  cell;
    }else if(self.messageArray[indexPath.section].messageType == -2){
        ChatImageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChatImageCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.message = self.messageArray[indexPath.section];
        cell.transform = CGAffineTransformMakeScale(1, -1);
        return  cell;
    }
    
    return [UITableViewCell new];
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.messageArray[indexPath.section].messageType == -2) {
        
//        BaseMessageModel *model = self.messageArray[indexPath.section];
//        NSMutableArray *datas = [NSMutableArray array];
//        
//        [self.messageArray enumerateObjectsUsingBlock:^(NSString *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//            // 网络图片
//            YBIBImageData *data = [YBIBImageData new];
//            data.imageURL = [NSURL URLWithString:obj];
//            data.projectiveView = [self viewAtIndex:idx];
//            [datas addObject:data];
//            
//        }];
//        
//        YBImageBrowser *browser = [YBImageBrowser new];
//        browser.dataSourceArray = datas;
//        browser.currentPage = indexPath.row;
//        browser.defaultToolViewHandler.topView.operationType = YBIBTopViewOperationTypeSave;
//        [browser show];
    }else
    {
        BaseMessageModel *model = self.messageArray[indexPath.section];
        if ([self.vcdelegate respondsToSelector:@selector(rtcChatForbiddenWithPerson:)]) {
            [self.vcdelegate rtcChatForbiddenWithPerson:model];
        }
    }
}

- (id)viewAtIndex:(NSInteger)index {
    ChatImageCell *cell = (ChatImageCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]];
    return cell ? cell.contentView : nil;
}



//-(void)refreshGift{
//    BaseMessageModel *model = _messageArray[0];
//    if(model.isGift){
//        [self.time invalidate];
//        self.time = nil;
//        self.giftView.model = model;
//        self.giftView.frame = CGRectMake(-(SCREEN_WIDTH-105), 0, SCREEN_WIDTH-105, 70);
//        self.giftView.alpha = 1;
//        [UIView animateWithDuration:0.5 animations:^{
//            self.giftView.frame = CGRectMake(0, 0, SCREEN_WIDTH-105, 70);
//        } completion:^(BOOL finished) {
//            self.time = [NSTimer scheduledTimerWithTimeInterval:2 repeats:false block:^(NSTimer * _Nonnull timer) {
//                [UIView animateWithDuration:1 animations:^{
//                    self.giftView.alpha = 0;
//                } completion:^(BOOL finished) {
//                    self.giftView.alpha = 1;
//                    self.giftView.frame = CGRectMake(-(SCREEN_WIDTH-105), 0, SCREEN_WIDTH-105, 70);
//                }];
//            }];
//        }];
//    }
//}

@end

